SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE dbo.GetAllTradesLive AS
BEGIN
SELECT  p.FundId ,
        p.PortfolioId ,
        i.InstrName ,
		a.EndAdjAmt,
		a.MktVal,
		a.IsActiveEndOfDay,
		 
		       		        t.TradeDate ,
		        t.TradeType ,
		        t.SettlementDate ,
		        t.TradeAmount ,
		        t.Proceeds ,
		        t.TradePrice ,
		        t.Shares ,
		        t.IsSettled ,
		        t.AmortCost ,
		        t.RealGainLoss ,
		        t.BlotterId 

FROM    FSFundModel.Reporting.Trade t
        INNER JOIN FSFundModel.Reporting.Asset a ON a.ExportId = t.ExportId
                                                    AND a.PortfolioId = t.PortfolioId
                                                    AND t.InstrumentId = a.InstrumentId
                                                    AND t.LotId = a.LotId
        INNER JOIN FSFundModel.Reporting.Instrument i ON t.ExportId = i.ExportId
                                                         AND t.InstrumentId = i.InstrumentId
        INNER JOIN FSFundModel.Reporting.Portfolio p ON t.ExportId = p.ExportId
                                                        AND t.PortfolioId = p.PortfolioId
        INNER JOIN FSFundModel.Reporting.Fund f ON t.ExportId = f.ExportId
                                                   AND p.FundId = f.FundId
WHERE   t.ExportId IN ( SELECT  MAX(e.ExportId)
                        FROM    FSFundModel.Reporting.Export e
                        WHERE   e.FundId IN ( 'FSIC', 'FSEPF', 'FSIC2' )
                        GROUP BY e.FundId )
ORDER BY f.FundIntrnlId ASC ,
        p.SortOrder ,
        i.InstrName ASC
END

GO
EXEC sp_addextendedproperty N'VirtualFolder', N'Dbo', 'SCHEMA', N'dbo', 'PROCEDURE', N'GetAllTradesLive', NULL, NULL
GO
